import Index from '../pages/index.js';
import Router from './director.js';
// import Infos from '../pages/infos.js';
import Login from '../pages/login.js';
import Films from '../pages/films.js';
import Users from '../pages/users.js';
import Fac from '../pages/fac.js';
import Cinemas from '../pages/cinemas.js'
import Soons from '../pages/soons.js';
import HotShow from '../pages/hotShow.js';
import HotPlay from '../pages/hotPlay.js';

let { $ } = layui;

let routes = {
    '/login': () => new Login(),
    '/index': {
        '/fac': () => new Fac(),
        '/users': () => new Users(),
        '/cinemas': () => new Cinemas(),
        '/soons': () => new Soons(),
        // '/infos': () => new Infos(),
        '/films': () => new Films(),
        '/hotShow': () => new HotShow(),
        '/hotPlay': () => new HotPlay(),
        on: (next) => new Index(next),
    }
};

$.ajaxSettings.beforeSend = function (xhr) {
    let token = localStorage.getItem('token');
    xhr.setRequestHeader('Authorization', 'Bearer ' + token);
}

$.ajaxSettings.error = function (err) {
    if (err.status == 401) {
        layer.alert('请登录');
        location.href = '#login';
    }
}

location.hash = location.hash || 'login';

let router = Router(routes).configure({
    recurse: 'forward',
    async: true,
    strict: false
})

router.init();